#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
读取全局配置文件
"""
import configparser
from pathlib import Path

from beanie import init_beanie
from motor.motor_asyncio import AsyncIOMotorClient

from auth.permission.model import Permission
from auth.role.model import Role
from auth.user.model import User

conf_global = configparser.ConfigParser()
parent_dir = Path(__file__).parent
# print(parent_dir)
conf_global.read(parent_dir / 'app.conf', encoding='utf-8')


async def init_database():
    ip = conf_global.get('mongo', 'ip')
    port = conf_global.getint('mongo', 'port')
    db = conf_global.get('mongo', 'db')
    username = conf_global.get('mongo', 'username')
    password = conf_global.get('mongo', 'password')
    client = AsyncIOMotorClient(f"mongodb://{username}:{password}@{ip}:{port}/{db}")
    await init_beanie(database=client.get_default_database(),
                      document_models=[Permission, Role, User])


if __name__ == '__main__':
    print(conf_global.get('mongo', 'db'))
